# THIS SCRIPT COMPUTES SUMMARY STATISTICS ON SELF-REPORTED VOTING IN THE
# GENERAL ELECTION AND PRESIDENTIAL PRIMARIES, PRESENTED IN TABLES 4 AND 5. 
# IT MUST BE RUN USING THE FILE "ANESCumulative_cleaned.csv," AVAILABLE
# IN THE REPLICATION MATERIALS 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
####################################
####################################
####################################
####################################
####################################
####################################
####################################
# TABLE 4: SELF-REPORTED TURNOUT DATA, 1972-2020: PROPORTION OF MATCHED
# PARTISANS AMONG PARTISANS THAT REPORTED VOTING IN THE 
# PRESIDENTIAL PRIMARIES AND GENERAL ELECTION

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# PRESIDENTIAL PRIMARIES: % MATCHED AMONG PARTISANS THAT SELF-REPORTED VOTING,
our.props = svytable(~ programmatic + year, design = subset(our.design, primary.vote==1))
our.props = prop.table(our.props, 2)
our.props = as.data.frame(our.props)
our.props$year = as.numeric(as.character(our.props$year))
our.props.primaries = our.props
our.props.primaries = subset(our.props.primaries, programmatic==1)
our.props.primaries$Freq = round(our.props.primaries$Freq, 2)
our.props.primaries

# GENERAL ELECTION: % MATCHED AMONG PARTISANS THAT SELF-REPORTED VOTING
our.props = svytable(~ programmatic + year, design = subset(our.design, voted.general==1))
our.props = prop.table(our.props, 2)
our.props = as.data.frame(our.props)
our.props$year = as.numeric(as.character(our.props$year))
our.props.general = our.props
our.years = seq(1972, 2020, by = 4)
our.props.general = subset(our.props.general, year %in% our.years)
our.props.general = subset(our.props.general, programmatic==1)
our.props.general$Freq = round(our.props.general$Freq, 2)
our.props.general
####################################
####################################
####################################
####################################
####################################
####################################
####################################
####################################
# TABLE 5: SELF-REPORTED TURNOUT DATA, 1972-2020, BY PARTISANSHIP: 
# PROPORTION OF MATCHED PARTISANS AMONG PARTISANS THAT REPORTED VOTING IN THE 
# PRESIDENTIAL PRIMARIES AND GENERAL ELECTION

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# PRESIDENTIAL PRIMARIES: % MATCHED AMONG PARTISANS THAT SELF-REPORTED VOTING,
# BROKEN DOWN BY PARTISANSHIP 
our.props = svytable(~ programmatic + year + parties3, design = subset(our.design, primary.vote==1))
our.props = prop.table(our.props, 2:3)
our.props = as.data.frame(our.props)
our.props$year = as.numeric(as.character(our.props$year))
our.props.primaries = our.props
our.props.primaries
our.props.primaries = subset(our.props.primaries, programmatic==1)
our.props.primaries$Freq = round(our.props.primaries$Freq, 2)
our.props.primaries

# GENERAL ELECTION: % MATCHED AMONG PARTISANS THAT SELF-REPORTED VOTING,
# BROKEN DOWN BY PARTISANSHIP 
our.props = svytable(~ programmatic + year + parties3, design = subset(our.design, voted.general==1))
our.props = prop.table(our.props, 2:3)
our.props = as.data.frame(our.props)
our.props$year = as.numeric(as.character(our.props$year))
our.props.general = our.props
our.props.general
  our.years = seq(1972, 2020, by = 4)
our.props.general = subset(our.props.general, year %in% our.years)
our.props.general = subset(our.props.general, programmatic==1)
our.props.general$Freq = round(our.props.general$Freq, 2)
our.props.general
